﻿using System;
using System.Collections.Generic;
using System.Linq;
using Twitterizer;

namespace MishraReader.Entities
{
    public sealed class TwitterShareService : ShareServiceBase
    {
        public const string CallbackUri = "http://www.catuhe.com";
        public const string ConsumerKey = "ZFwWMB0c5gRruBTi7ydGg";
        public const string ConsumerKeySecret = "IwABdiSlTu3n2jsps5y8LnVaM7ufTHLdqNcImxygQ";

        public override bool IsSupported
        {
            get { return true; }
        }

        public TwitterShareService()
        {
            ServiceName = "Twitter";
            ServiceId = new Guid("D73653C3-1A7C-4171-887B-FA5C81CD1C6F");
            ServiceType = ServiceType.Share;
        }

        //   public const string RequestTokenUri = "https://api.twitter.com/oauth/request_token";
        //   public const string OAuthVersion = "1.0a";
        //  public const string AuthorizeUri = "https://api.twitter.com/oauth/authorize";
        //  public const string AccessTokenUri = "https://api.twitter.com/oauth/access_token";

        public override void Share(ShareServiceAccount account, string message, Item item)
        {
            var tokens = new OAuthTokens
                             {
                                 AccessToken = account.Token,
                                 AccessTokenSecret = account.Password,
                                 ConsumerKey = ConsumerKey,
                                 ConsumerSecret = ConsumerKeySecret
                             };

            TwitterResponse<TwitterStatus> tweetResponse;
            if (message.Length <= 119)
            {
                tweetResponse = TwitterStatus.Update(tokens, message + " " + item.PostUri.AbsoluteUri);
            }
            else
            {
                var currentline = "";
                foreach (var splittedmessage in message.Split(' '))
                {
                    if (splittedmessage.Length > 140)
                        return;
                    var tempcurrentline = currentline + (currentline == String.Empty ? "" : " ") + splittedmessage;

                    if (tempcurrentline.Length <= 140)
                        currentline = tempcurrentline;
                    else
                    {
                        tweetResponse = TwitterStatus.Update(tokens, currentline);
                        currentline = splittedmessage;
                    }
                }

                if (currentline.Length <= 119)
                    tweetResponse = TwitterStatus.Update(tokens, currentline + " " + item.PostUri.AbsoluteUri);
                else
                {
                    if (currentline != string.Empty)
                        tweetResponse = TwitterStatus.Update(tokens, currentline);

                    tweetResponse = TwitterStatus.Update(tokens, item.PostUri.AbsoluteUri);
                }
            }
        }
    }
}